﻿using AgileLogistics.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using WPFMediaKit.DirectShow.Controls;

namespace AgileLogistics.UI
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        ViewModel.HomeViewModel vm;
        Model.DBContext dBContext = new DBContext();
        public MainWindow()
        {
            InitializeComponent();

           var loginvm = GalaSoft.MvvmLight.Ioc.SimpleIoc.Default.GetInstance<ViewModel.MainViewModel>();
            vm = DataContext as ViewModel.HomeViewModel;

            vm.LoginUser = Comm.Temp.LoginUser;
            vm.CurrDep = dBContext.DB.Queryable<Depment>().Where(d => d.Id == vm.LoginUser.DepId).First();
            vm.DepUserCount = dBContext.DB.Queryable<Users>().Where(u => u.DepId == vm.LoginUser.DepId).Count();
            vm.CurrRole = dBContext.DB.Queryable<Roles>().Where(r =>r.Id ==vm.LoginUser.RoleId).First();


            //验证权限
         
            if (vm.CurrRole.FuncNotice)
            {
                btn_Nocite.Visibility = Visibility.Visible;
            }

            if(vm.CurrRole.FuncCailiao && vm.CurrRole.FuncMenu && vm.CurrRole.FuncNotice && vm.CurrRole.FuncWeixiu)
            {
                btn_Base.Visibility = Visibility.Visible;
            }
            else
            {
                btn_Base.Visibility = Visibility.Collapsed;
            }


            //=================================获取实时天气=================>
            //天气

            GetWeather();

            //=================================获取实时天气=================>


            
            
        }



        private void GetWeather()
        {
            try
            {
                var url = "http://apis.juhe.cn/simpleWeather/query";
                var key = "cd93d46a94a97598e1aedaecaccae6a5";

                var client = new RestSharp.RestClient(url);

                var req = new RestSharp.RestRequest(RestSharp.Method.GET);
                req.AddParameter("key", key);
                req.AddParameter("city", "安阳");

                var resp = client.Execute(req);

                var w = Newtonsoft.Json.JsonConvert.DeserializeObject<Models.WContent>(resp.Content);

                vm.Weather = w.result.realtime;
            }
            catch (Exception)
            {

                vm.Weather = new Models.RealTimeWeather() { info = "获取失败" };
            }

        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
           DBContext context = new AgileLogistics.Model.DBContext();
            //var list = context.DB.Queryable<Roles>().ToList() ;
            // var   list =  new Repository<Roles>().GetList();

           

            //foreach ( var item in list)
            //{
            //    MessageBox.Show(item.Name);
            //}
            
        }

        private void btn_Home_Click(object sender, RoutedEventArgs e)
        {
            Frm.Navigate(new AgileLogistics.View.Home.Home());
        }

        private void btn_Base_Click(object sender, RoutedEventArgs e)
        {
            Frm.Navigate(new AgileLogistics.View.Base.Page_Base());
        }

        private void btn_User_Click(object sender, RoutedEventArgs e)
        {
            Frm.Navigate(new AgileLogistics.View.User.Page_List());
        }

        private void btn_Baoxiu_Click(object sender, RoutedEventArgs e)
        {
            Frm.Navigate(new AgileLogistics.View.Baoxiu.Page_Baoxiu());
        }

        private void btn_Cailiao_Click(object sender, RoutedEventArgs e)
        {
            Frm.Navigate(new AgileLogistics.View.Cailiao.Page_Cailiao());
        }

        private void btn_Menu_Click(object sender, RoutedEventArgs e)
        {
            Frm.Navigate(new AgileLogistics.View.Meishi.Page_Meishi());
        }

        private void btn_Cancle_Click(object sender, RoutedEventArgs e)
        {
            Comm.Temp.LoginUser = null;
            Login win = new Login();
            win.Show();
            this.Close();


            //dBContext.DB.CodeFirst.InitTables<Model.Roles>();

        }

        private void btn_Nocite_Click(object sender, RoutedEventArgs e)
        {
             Frm.Navigate(new AgileLogistics.View.Nocite.Page_Notice());

            //dBContext.DB.CodeFirst.InitTables<Model.Notice>();
            //dBContext.DB.CodeFirst.InitTables<Model.NoticeUsers>();
        }
    }
}
